//
//  CZEmoticonKeyBoard.swift
//  WeiBo
//
//  Created by Apple on 16/9/8.
//  Copyright © 2016年 KY. All rights reserved.
//

import UIKit

//  表情键盘视图
/*
    1. 表情视图 -> UICollectionView
 
    2. ToolBar -> 工具条
 
    3. 页数指示器 -> UIPageControl
 */


//  表情键盘的高度
let EmoticonKeyBoardHeight: CGFloat = 216
//  toolbar的高度
let EmoticonToolBarHeight: CGFloat = 35

//  表情视图的高度
let EmoticonCollectionViewHeight = EmoticonKeyBoardHeight - EmoticonToolBarHeight

class CZEmoticonKeyBoard: UIView {

    
    // MARK:    --懒加载控件
    private lazy var toolBar: CZEmoticonToolBar = {
        let view = CZEmoticonToolBar()
        return view
    }()
    
    //  表情视图,这样写需要把itemsize在初始化的时候就设置好，不能在自己的layoutsubviews里面写会重复加载
    private lazy var emoticonCollectionView: CZEmoticonCollectionView = {
        CACurrentMediaTime()
        let view = CZEmoticonCollectionView()
        
        view.backgroundColor = self.backgroundColor
        return view
    }()

//    private lazy var emoticonCollectionView: UICollectionView = {
//        
//        let flowLayout = UICollectionViewFlowLayout()
//        let view = UICollectionView(frame: CGRect.zero, collectionViewLayout: flowLayout)
//        view.backgroundColor = self.backgroundColor
//        
//        
//        //  每项之间的水平间距
//        flowLayout.minimumInteritemSpacing = 0
//        //  每项之间的垂直间距
//        flowLayout.minimumLineSpacing = 0
//        
//        //  设置滚动放心
//        flowLayout.scrollDirection = .horizontal
//        //  设置分页
//        view.isPagingEnabled = true
//        //  关闭弹簧效果
//        view.bounces = false
//        //  滚动指示器
//        view.showsVerticalScrollIndicator = false
//        view.showsHorizontalScrollIndicator = false
//        
//        //  设置数据源代理
//        view.dataSource = self
//        
//        //  注册cell
//        view.register(CZEmoticonCollectionViewCell.self, forCellWithReuseIdentifier: CZEmoticonCollectionViewCellIdentifier)
//        
//        return view
//    }()
//
    
    //  页数指示器
    private lazy var pageControl: UIPageControl = {
        let pageCtr = UIPageControl()
        pageCtr.currentPageIndicatorTintColor = UIColor(patternImage: UIImage(named: "compose_keyboard_dot_selected")!)
        pageCtr.pageIndicatorTintColor = UIColor(patternImage: UIImage(named: "compose_keyboard_dot_normal")!)
        //  只有一页不显示
        pageCtr.hidesForSinglePage = true
    
        return pageCtr
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        
        
        //  让其等到主线程执行完成在执行该代码，主线程异步
        DispatchQueue.main.async {
            //  默认让其滚动到第二组
            let normalIndexPath = IndexPath(item: 0, section: 1)
            //  不开启动画
            self.emoticonCollectionView.scrollToItem(at: normalIndexPath, at: .left, animated: false)
            //  绑定页数数据
            self.setPageControlDataForIndexPath(indexPath: normalIndexPath)
        }
        
        
        //  设置背景图片
        backgroundColor = UIColor(patternImage: UIImage(named: "emoticon_keyboard_background")!)
        //  添加控件
        addSubview(emoticonCollectionView)
        addSubview(pageControl)
        addSubview(toolBar)

        //  设置滚动选中按钮的闭包
        emoticonCollectionView.scrollClosure = { [weak self] (indexPath) in
            //  根据index的section选中相应的按钮
            self?.toolBar.selectedButtonWithSection(section: indexPath.section)
            //  滚动的时候绑定页数指示器
            self?.setPageControlDataForIndexPath(indexPath: indexPath)
        }
        
        
        //  添加控件约束
        
        emoticonCollectionView.snp_makeConstraints { (make) in
            make.top.equalTo(self)
            make.leading.equalTo(self)
            make.trailing.equalTo(self)
            make.bottom.equalTo(toolBar.snp_top)
        }
        
        pageControl.snp_makeConstraints { (make) in
            make.bottom.equalTo(emoticonCollectionView)
            make.centerX.equalTo(emoticonCollectionView)
            make.height.equalTo(10)
        }
        
        toolBar.snp_makeConstraints { (make) in
            make.left.equalTo(self)
            make.bottom.equalTo(self)
            make.trailing.equalTo(self)
            make.height.equalTo(EmoticonToolBarHeight)
        }
        
        //  设置toolbar的闭包
        
        toolBar.didSelectedButtonClosure = { [weak self] (type) in
            let indexPath: IndexPath
            switch type {
            case .recent:
                print("最近")
                indexPath = IndexPath(item: 0, section: 0)
            case .normal:
                print("默认")
                indexPath = IndexPath(item: 0, section: 1)
            case .emoji:
                print("emoji")
                indexPath = IndexPath(item: 0, section: 2)
            case .lxh:
                print("浪小花")
                indexPath = IndexPath(item: 0, section: 3)
            }
            //  滚动到执行的indexpath, 注意不用开启动画
            self?.emoticonCollectionView.scrollToItem(at: indexPath, at: .left, animated: false)
            //  
            self?.setPageControlDataForIndexPath(indexPath: indexPath)
            
            
        }
    }
 
    
    //  绑定页数指示器数据
    private func setPageControlDataForIndexPath(indexPath: IndexPath) {
        //  每组数据显示的个数
        pageControl.numberOfPages = CZEmoticonTools.shareTools.allEmoticonArray[indexPath.section].count
        //  当前页数的索引
        pageControl.currentPage = indexPath.item
    }

//    override func layoutSubviews() {
//        super.layoutSubviews()
//        print(CACurrentMediaTime())
//        let flowLayout = emoticonCollectionView.collectionViewLayout as! UICollectionViewFlowLayout
//        //  每项的大小等于当前视图的大小
//        flowLayout.itemSize = emoticonCollectionView.size
//    }
    
}

// MARK:    --UICollectionViewDataSource
//extension CZEmoticonKeyBoard: UICollectionViewDataSource {
//    func numberOfSections(in collectionView: UICollectionView) -> Int {
//        return CZEmoticonTools.shareTools.allEmoticonArray.count
//    }
//    
//    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
//        return CZEmoticonTools.shareTools.allEmoticonArray[section].count
//    }
//    
//    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
//        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CZEmoticonCollectionViewCellIdentifier, for: indexPath) as! CZEmoticonCollectionViewCell
//        cell.indexPath = indexPath
//        cell.emoticonsArray = CZEmoticonTools.shareTools.allEmoticonArray[indexPath.section][indexPath.item]
//        return cell
//    }
//}



















